
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<script type="text/javascript" src="javascript/Gamepackage/Engine.js"></script>
		<script type="text/javascript">
			Gamepackage.setPath("javascript/Gamepackage");
		</script>
		<title></title>
		<script type="text/javascript">
			var mainCanvas;
			var world;
			var mcg;
			var cWidth, cHeight;
			var enemyImg, mapImg, towerImg;
			
			function loadProgramFile(callback) {
				Gamepackage.loadClasses([
				"Runner",
				"ui.g2d.physics.Physics",
				"ui.g2d.Displayable",
				"ui.g2d.Graphics",
				"ui.g2d.Canvas",
				"ui.g2d.Sprite",
				"ui.g2d.Map",
				"ui.g2d.Image",
				"ui.g2d.Layer",
				"ui.Controller",
				],function () {
					Gamepackage.getScripts([
					"javascript/Enemy.js",
					"javascript/World.js",
					"javascript/LinkedList.js",
					"javascript/EnemyGroup.js",
					"javascript/Tower.js",
					"javascript/IceTower.js"
					], callback);
				});
			};
			
			function addEnemy(speed) {
				//test googlesvn comment.
				world.anEnemyGo(new Enemy(enemyImg, 16, 16, speed));
			};
			
			function loadResource(callback) {
				enemyImg = new Gamepackage.ui.g2d.Image("images/enemy2.png");
				mapImg = new Gamepackage.ui.g2d.Image("images/map.png");
				towerImg = new Gamepackage.ui.g2d.Image("images/towers.png");
				Gamepackage.loadImage([enemyImg, mapImg, towerImg], function () {
					if(callback)
						callback();
				});
			};
			
			function init() {
				//init objects.
				mainCanvas = new Gamepackage.ui.g2d.Canvas(document.getElementById("canvas"), 960, 640);
				//绘图对象
				mcg = mainCanvas.getGraphics();
				//高宽缓存
				cWidth = mainCanvas.getWidth();
				cHeight = mainCanvas.getHeight();
				//地图数组
				var mapArr = [
				[4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
				[4, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1],
				[4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1],
				[4, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1],
				[4, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
				[4, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1],
				[4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 1],
				[4, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1],
				[4, 1, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
				[4, 1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3]
				];
				
				//normal tower
				world = new World(new Gamepackage.ui.g2d.Map(mapImg, mapArr, 64, 64, 960, 640));
				world.addTowerPrototype(new Tower(towerImg, 63, 63, 120, 1));
				//ice tower
				var iceTower = new IceTower(towerImg, 63, 63, 120, 1, null, Tower.MODE_AREA, 2);
				world.addTowerPrototype(iceTower);
				//初始化控制器
				Gamepackage.ui.Controller.setTarget(mainCanvas.getSourceCanvas());
				//start runner
				var fps = 0;
				
				addNormalEnemyGroup(2000, 4, 100, 1000, 800);
				
				new Gamepackage.Runner(function () {
						++ fps;
						userAction();
						gameAction();
						paint(mcg);
					}, 33).start();
				new Gamepackage.Runner(function () {
						printFps(fps);
						fps = 0;
					}, 1000).start();
			};
			
			function addNormalEnemyGroup (num, speed, hp, startTime, delayTime) {
				var nextBornTime = new Date().getTime() + startTime;
				for(var i=0;i<num;++i)
				{
					world.addEnemy(new Enemy(enemyImg, 16, 16, speed, null, null, nextBornTime, hp));
					nextBornTime += delayTime;
				}
			};
			
			//开始游戏进程
			function start() {
				//现在加载程序文件再加载资源最后初始化对象
				loadProgramFile(function () {
					loadResource(init);
				});
			};
			
			function paint(g) {
				g.fillRect("rgb(255, 255, 255)", 0, 0, cWidth, cHeight);
				world.draw(g);
				drawTowerLayTip(g);
			};
			
			function drawTowerLayTip(g) {
				if(towerLayX && towerLayY)
				{
					var map = world.getMap();
					g.context.lineWidth = "2";
					g.drawRect("#000000", towerLayX, towerLayY, map.getBlockWidth(), map.getBlockHeight());
					g.fillRect("rgba(98, 237, 78, 0.5)", towerLayX + 1, towerLayY + 1, map.getBlockWidth() - 2, map.getBlockHeight() - 2);
				}
			};
			
			//用户操作处理: fetch tower var.
			var fetchingTower = null;
			var fetchingTowerSX = null, fetchingTowerSY = null;
			var fetchingTowerOffsetX = null, fetchingTowerOffsetY = null;
			var towerLayX = null, towerLayY = null;
			
			function userAction() {
				var mousemove = Gamepackage.ui.Controller.popMouseEvent("mousemove");
				var mousedown = Gamepackage.ui.Controller.popMouseEvent("mousedown");
				var mouseup = Gamepackage.ui.Controller.popMouseEvent("mouseup");
				if(mousedown)
				{
					fetchTower(mousedown.viewX, mousedown.viewY);
				}
				if(mouseup)
				{
					if(fetchingTower)
						cancelFetchTower();
				}
				if(mousemove) 
				{
					if(fetchingTower) 
						moveFetchingTower(mousemove.viewX, mousemove.viewY);
				}
				Gamepackage.ui.Controller.cleanAllMouseEvent();
			};
			
			function fetchTower (x, y){
				fetchingTower = world.fetchTower(x, y);
				if(fetchingTower)
				{
					fetchingTowerSX = fetchingTower.getX();
					fetchingTowerSY = fetchingTower.getY();
					fetchingTowerOffsetX = x - fetchingTowerSX;
					fetchingTowerOffsetY = y - fetchingTowerSY;
					fetchingTower.fatch();
					towerLayX = null;
					towerLayY = null;
				}
			};
			
			function cancelFetchTower () {
				if(towerLayX && towerLayY)
				{
					var newTower = fetchingTower.clone();
					newTower.setX(towerLayX);
					newTower.setY(towerLayY);
					world.addTower(newTower);
				}
				fetchingTower.setX(fetchingTowerSX);
				fetchingTower.setY(fetchingTowerSY);
				fetchingTower.cancelFetch();
				fetchingTower = null;
				towerLayX = null;
				towerLayY = null;
			};
			
			function moveFetchingTower (x, y) {
				fetchingTower.setX(x - fetchingTowerOffsetX);
				fetchingTower.setY(y - fetchingTowerOffsetY);
				var map = world.getMap();
				var block = map.blockCollisionWithRect(fetchingTower.getX(), fetchingTower.getY(), 1, 1);
				if(block && block.frame == World.MAPBLOCK_WALL)
				{
					towerLayX = block.x + 1;
					towerLayY = block.y + 1;
				}
				else
				{
					towerLayX = null;
					towerLayY = null;
				}
				var towerList = world.getTowerList();
				var towerListLength = towerList.length;
				for(var i=0;i<towerListLength;++i)
				{
					if(towerList[i].collisionWithRect(towerLayX, towerLayY, fetchingTower.getWidth(), fetchingTower.getHeight()))
					{
						towerLayX = null;
						towerLayY = null;
					}
				}
			};
			
			//游戏行为处理
			function gameAction() {
				world.working();
			};
			
			onload = function () {
				start();
			};
			
			function print(msg) {
				document.getElementById("msg").innerHTML = msg;
			};
			
			function printFps(msg) {
				document.getElementById("fps").innerHTML = "fps: " + msg;
			};
			
			function cleanPrint() {
				document.getElementById("msg").innerHTML = " ";
			};
		</script>
        <style type="text/css">
        	#msgBar {
        		text-align: center;
				font-size: 12px;
				margin-bottom: 16px;
        	}
			
			.mb-block {
				margin-left: 16px;
			}
        </style>
	</head>
    <body>
    	<div id="msgBar">
    		<span class="mb-block"><span>还可以漏</span><span id="playerHp"></span>20<span>个敌人</span></span>
			<span class="mb-block"><span>金钱:</span><span id="playerGold">0</span></span>
    	</div>
    	<center>
    		<canvas id="canvas" style="border: 1px solid #cccccc;"></canvas>
    	</center>
    	<div id="fps"></div>
    	<div id="msg"></div>
    </body>
</html>
